package com.lanou.connotationjoke.adapter;

import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.db.HomeCollectEntity;
import com.lanou.connotationjoke.entity.LargeImageEty;
import com.lanou.connotationjoke.tool.MyAppllication;
import com.lanou.connotationjoke.tool.ShareSingleton;
import com.lanou.connotationjoke.tool.VolleySingleton;

import java.util.List;

/**
 * Created by Administrator on 2016/2/5.
 */
public class HomeCollectListViewAdapter extends BaseAdapter{
    private Context context;
    private List<HomeCollectEntity> datas;

    public HomeCollectListViewAdapter(Context context, List<HomeCollectEntity> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        HomeCollectEntity homeCollectEntity = datas.get(position);
        if (homeCollectEntity.getMp4_url() != null){
            return 2;
        }else if (homeCollectEntity.getUri() != null){
            return 1;
        }else {
         return 0;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);
        JokeViewHolder jokeViewHolder = null;
        PictureViewHolder pictureViewHolder = null;
        VideoViewHolder videoViewHolder = null;
        if (convertView == null){
            switch (type){
                case 0:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_recycleview_home_joke,null);
                    jokeViewHolder = new JokeViewHolder(convertView);
                    convertView.setTag(jokeViewHolder);
                    break;
                case 1:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_picture,null);
                    pictureViewHolder = new PictureViewHolder(convertView);
                    convertView.setTag(pictureViewHolder);
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_video,null);
                    videoViewHolder = new VideoViewHolder(convertView);
                    convertView.setTag(videoViewHolder);
                    break;
            }
        }else {
            switch (type){
                case 0:
                    jokeViewHolder = (JokeViewHolder) convertView.getTag();
                    break;
                case 1:
                    pictureViewHolder = (PictureViewHolder) convertView.getTag();
                    break;
                case 2:
                    convertView = LayoutInflater.from(context).inflate(R.layout.item_listview_home_video,null);
                    videoViewHolder = new VideoViewHolder(convertView);
                    break;
            }
        }
        final HomeCollectEntity homeCollectEntity = datas.get(position);
        switch (type){
            case 0:
                jokeViewHolder.collectTv.setVisibility(View.GONE);
                jokeViewHolder.diggTv.setText("" + homeCollectEntity.getDigg_count());
                jokeViewHolder.contentTv.setText(homeCollectEntity.getContent());
                jokeViewHolder.userNameTv.setText(homeCollectEntity.getName());
                jokeViewHolder.categoryName_Tv.setText(homeCollectEntity.getCategory_name());
                jokeViewHolder.buryTv.setText("" + homeCollectEntity.getBury_count());
                jokeViewHolder.hotCommentTv.setText("" + homeCollectEntity.getComment_count());
                jokeViewHolder.sendTv.setText("" + homeCollectEntity.getShare_count());
                jokeViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                jokeViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                jokeViewHolder.userPictureIv.setImageUrl(homeCollectEntity.getAvatar_url()
                        ,VolleySingleton.getImageLoader());
                jokeViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()) {
                            String url = "http://neihanshequ.com/p" + homeCollectEntity.getShare_url().substring(37);
                            WebViewActivity.goToWebViewActivity(context, url);
                        } else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                jokeViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            String title = "超级搞笑";
                            String titleurl = "http://neihanshequ.com/p" + homeCollectEntity.getShare_url().substring(37);
                            String content = homeCollectEntity.getContent();
                            String imageurl = homeCollectEntity.getAvatar_url();
                            ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                        }else {
                            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case 1:
                pictureViewHolder.collectTv.setVisibility(View.GONE);
                if (MyAppllication.hasNetwork()) {
                    Glide.with(context).load(homeCollectEntity.getUri()).into(pictureViewHolder.largeIv);
                }
                pictureViewHolder.diggTv.setText("" + homeCollectEntity.getDigg_count());
                pictureViewHolder.contentTv.setText(homeCollectEntity.getContent());
                pictureViewHolder.userNameTv.setText(homeCollectEntity.getName());
                pictureViewHolder.categoryName_Tv.setText(homeCollectEntity.getCategory_name());
                pictureViewHolder.buryTv.setText("" + homeCollectEntity.getBury_count());
                pictureViewHolder.hotCommentTv.setText("" + homeCollectEntity.getComment_count());
                pictureViewHolder.sendTv.setText("" + homeCollectEntity.getShare_count());
                pictureViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                pictureViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                pictureViewHolder.userPictureIv.setImageUrl(homeCollectEntity.getAvatar_url()
                        , VolleySingleton.getImageLoader());
                pictureViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()) {
                            String url = "http://neihanshequ.com/p" + homeCollectEntity.getShare_url().substring(37);
                            WebViewActivity.goToWebViewActivity(context, url);
                        } else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                pictureViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            String title = "超级搞笑";
                            String titleurl = "http://neihanshequ.com/p" + homeCollectEntity.getShare_url().substring(37);
                            String content = homeCollectEntity.getContent();
                            String imageurl = homeCollectEntity.getAvatar_url();
                            ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                        }else {
                            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case 2:
                videoViewHolder.collectTv.setVisibility(View.GONE);
                videoViewHolder.playCountTv.setText(""+homeCollectEntity.getPlay_count());
                videoViewHolder.diggTv.setText("" + homeCollectEntity.getDigg_count());
                videoViewHolder.contentTv.setText(homeCollectEntity.getContent());
                videoViewHolder.userNameTv.setText(homeCollectEntity.getName());
                videoViewHolder.categoryName_Tv.setText(homeCollectEntity.getCategory_name());
                videoViewHolder.buryTv.setText("" + homeCollectEntity.getBury_count());
                videoViewHolder.hotCommentTv.setText("" + homeCollectEntity.getComment_count());
                videoViewHolder.sendTv.setText("" + homeCollectEntity.getShare_count());
                videoViewHolder.userPictureIv.setDefaultImageResId(R.mipmap.ic_launcher);
                videoViewHolder.userPictureIv.setErrorImageResId(R.mipmap.ic_launcher);
                videoViewHolder.userPictureIv.setImageUrl(homeCollectEntity.getAvatar_url()
                        , VolleySingleton.getImageLoader());
                videoViewHolder.coverImageIv.setDefaultImageResId(R.mipmap.ic_launcher);
                videoViewHolder.coverImageIv.setErrorImageResId(R.mipmap.ic_launcher);
                videoViewHolder.coverImageIv.setImageUrl(homeCollectEntity.getCover_image_uri()
                        ,VolleySingleton.getImageLoader());
                final VideoViewHolder finalVideoViewHolder = videoViewHolder;
                videoViewHolder.coverImageIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            finalVideoViewHolder.playVideo.setVisibility(View.GONE);
                            finalVideoViewHolder.progressBar.setVisibility(View.VISIBLE);
                            finalVideoViewHolder.videoView.setVideoURI(Uri.parse(homeCollectEntity.getMp4_url()));
                            finalVideoViewHolder.videoView.setMediaController(new MediaController(context));
                            finalVideoViewHolder.videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                                @Override
                                public void onPrepared(MediaPlayer mp) {
                                    finalVideoViewHolder.frameLayout.setVisibility(View.GONE);
                                    finalVideoViewHolder.videoView.requestFocus();
                                    finalVideoViewHolder.videoView.start();
                                }
                            });
                            finalVideoViewHolder.videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                                @Override
                                public boolean onError(MediaPlayer mp, int what, int extra) {
                                    Toast.makeText(context, "播放失败,网速不好", Toast.LENGTH_SHORT).show();
                                    return true;
                                }
                            });
                            finalVideoViewHolder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                                @Override
                                public void onCompletion(MediaPlayer mp) {
                                    Toast.makeText(context, "视频播放完毕", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                videoViewHolder.contentTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            final String url = "http://neihanshequ.com/p"+homeCollectEntity.getShare_url().substring(37);
                            WebViewActivity.goToWebViewActivity(context, url);
                        }else {
                            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
                        }
                    }
                });


                videoViewHolder.shareIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (MyAppllication.hasNetwork()){
                            String title = "超级搞笑";
                            String titleurl = "http://neihanshequ.com/p" + homeCollectEntity.getShare_url().substring(37);
                            String content = homeCollectEntity.getContent();
                            String imageurl = homeCollectEntity.getAvatar_url();
                            ShareSingleton.getInstance().shareOnQQ(title, titleurl, content, imageurl);
                        }else {
                            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
        }
        return convertView;
    }



    class JokeViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        ImageView shareIv;
        TextView collectTv;
        public JokeViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_joke);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }


    class PictureViewHolder{
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        ImageView largeIv;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        ImageView shareIv;
        TextView collectTv;
        public PictureViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            largeIv = (ImageView) itemView.findViewById(R.id.iv_large_url);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_picture);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }



    class VideoViewHolder {
        NetworkImageView userPictureIv;
        TextView userNameTv;
        TextView contentTv;
        TextView categoryName_Tv;
        NetworkImageView coverImageIv;
        TextView playCountTv;
        VideoView videoView;
        TextView diggTv;
        TextView buryTv;
        TextView hotCommentTv;
        TextView sendTv;
        View frameLayout;
        ImageView shareIv;
        ImageView playVideo;
        ProgressBar progressBar;
        TextView collectTv;
        public VideoViewHolder(View itemView) {
            userPictureIv = (NetworkImageView) itemView.findViewById(R.id.iv_user_picture);
            userNameTv = (TextView) itemView.findViewById(R.id.tv_user_name);
            contentTv = (TextView) itemView.findViewById(R.id.tv_content);
            categoryName_Tv = (TextView) itemView.findViewById(R.id.tv_category_name);
            coverImageIv = (NetworkImageView) itemView.findViewById(R.id.iv_cover_image);
            playCountTv = (TextView) itemView.findViewById(R.id.tv_play_count);
            videoView = (VideoView) itemView.findViewById(R.id.video_view);
            diggTv = (TextView) itemView.findViewById(R.id.tv_digg_count);
            buryTv = (TextView) itemView.findViewById(R.id.tv_bury_count);
            hotCommentTv = (TextView) itemView.findViewById(R.id.tv_hot_comment_count);
            sendTv = (TextView) itemView.findViewById(R.id.tv_send_count);
            frameLayout = itemView.findViewById(R.id.framelayout);
            shareIv = (ImageView) itemView.findViewById(R.id.iv_share_home_video);
            playVideo = (ImageView) itemView.findViewById(R.id.iv_video_play);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progress_bar_home_video);
            collectTv = (TextView) itemView.findViewById(R.id.tv_collect);
        }
    }
}
